﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Data
{
    public class LogData
    {
        public void InsertLogCheckBalance(string CardNo)
        {
           using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("dbo.LogsInsert", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@LogTypeID", SqlDbType.Int).Value = 1;
                    cmd.Parameters.Add("@ATMID", SqlDbType.Int).Value = 2;
                    cmd.Parameters.Add("@CardNo", SqlDbType.NVarChar).Value = CardNo;
                    cmd.Parameters.Add("@LogDate", SqlDbType.NVarChar).Value = DateTimeOffset.Now.LocalDateTime;
                    cmd.Parameters.Add("@Amount", SqlDbType.Decimal).Value = 0;
                    cmd.Parameters.Add("@Details", SqlDbType.NVarChar).Value = "CheckBalance";
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }
        Connect cn = new Connect();
        LogTypeData lTD = new LogTypeData();
        ATMData aD = new ATMData();
        public List<LogsEntity> ReadLog(CardsEntity cE)
        {
            List<LogsEntity> listLog = new List<LogsEntity>();
            string sql = "dbo.LogsSelectAll";
            DataTable dt = cn.GetData(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["CardNo"].ToString() == cE.CardNo)
                {
                    LogsEntity lgE = new LogsEntity
                    {
                        LogID = (int)dt.Rows[i][0],
                        LogDate = (DateTime)dt.Rows[i]["LogDate"],
                        LogTypeID = (int)dt.Rows[i]["LogTypeID"],
                        CardNo = dt.Rows[i]["CardNo"].ToString(),
                        ATMID = (int)dt.Rows[i]["ATMID"],
                        Amount = (decimal)dt.Rows[i]["Amount"],
                        Details = dt.Rows[i]["Details"].ToString(),
                        LogType = lTD.GetLogType((int)dt.Rows[i]["LogTypeID"]),
                        ATM = aD.GetATM((int)dt.Rows[i]["ATMID"])
                    };
                    listLog.Add(lgE);
                }
            }
            return listLog;
        }
    }
    
}
